package com.farm.resume.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.farm.core.page.OperateType;
import com.farm.core.page.RequestMode;
import com.farm.core.page.ViewMode;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.parameter.FarmParameterService;
import com.farm.resume.domain.Resumebase;
import com.farm.resume.service.ResumebaseServiceInter;
import com.farm.web.WebUtils;
import com.farm.web.easyui.EasyUiUtils;

@RequestMapping({ "/resumebase" })
@Controller
public class ResumebaseController extends WebUtils {
	private static final Logger log = Logger.getLogger(ResumebaseController.class);
	@Resource
	ResumebaseServiceInter resumeBaseServiceImpl;

	public ResumebaseServiceInter getResumebaseServiceImpl() {
		return this.resumeBaseServiceImpl;
	}

	public void setResumebaseServiceImpl(ResumebaseServiceInter resumeBaseServiceImpl) {
		this.resumeBaseServiceImpl = resumeBaseServiceImpl;
	}

	@RequestMapping({ "/query" })
	@ResponseBody
	public Map<String, Object> queryall(DataQuery query, HttpServletRequest request) {
		try {
			query = EasyUiUtils.formatGridQuery(request, query);
			DataQuery dbQuery = DataQuery.init(query,
					"FARM_RESUME_BASE a left join FARM_RESUME_INTENSION b on a.userid=b.userid",
					"a.ID  as id,REGISTERED,DATEYEAR,DATEMONTH,REGISTEREDAREA,BIRTHDAY,SEX,a.PCONTENT  as PCONTENT,a.USERID  as USERID,NAME,a.PSTATE  as PSTATE,LIVESTRAREA,LIVESTR,QQCODE,WXCODE,MOBILECODE,EMAILCODE,MARRIAGESTA,NATIONALITY,OTHERTYPE,OTHERID,IDCODE,STUDYABROAD,ZZMM,WORKNATURE,WORKADDRAREA,DEGREEMAX,WORKADDR,WORKOCCUPATION,WORKINDUSTRY,WORKPAY,WORKSTAT");

			DataResult result = dbQuery.search();
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_MARRIAGESTA"), "MARRIAGESTA");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_SEX"), "SEX");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_NATIONALITY"), "NATIONALITY");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_ZZMM"), "ZZMM");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_STUDYABROAD"), "STUDYABROAD");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_DEGREE"), "DEGREEMAX");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_WORKNATURE"), "WORKNATURE");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_WORKOCCUPATION"),
					"WORKOCCUPATION");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_WORKINDUSTRY"),
					"WORKINDUSTRY");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_WORKPAY"), "WORKPAY");
			result.runDictionary(FarmParameterService.getInstance().getDictionary("RESUME_WORKSTAT"), "WORKSTAT");
			return ViewMode.getInstance().putAttrs(EasyUiUtils.formatGridData(result)).returnObjMode();
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.getMessage()).returnObjMode();
		}
	}

	@RequestMapping({ "/edit" })
	@ResponseBody
	public Map<String, Object> editSubmit(Resumebase entity, HttpSession session) {
		try {
			entity = this.resumeBaseServiceImpl.editResumebaseEntity(entity, getCurrentUser(session));
			return ViewMode.getInstance().setOperate(OperateType.UPDATE).putAttr("entity", entity).returnObjMode();
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setOperate(OperateType.UPDATE).setError(e.getMessage()).returnObjMode();
		}
	}

	@RequestMapping({ "/add" })
	@ResponseBody
	public Map<String, Object> addSubmit(Resumebase entity, HttpSession session) {
		try {
			entity = this.resumeBaseServiceImpl.insertResumebaseEntity(entity, getCurrentUser(session));
			return ViewMode.getInstance().setOperate(OperateType.ADD).putAttr("entity", entity).returnObjMode();
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setOperate(OperateType.ADD).setError(e.getMessage()).returnObjMode();
		}
	}

	@RequestMapping({ "/del" })
	@ResponseBody
	public Map<String, Object> delSubmit(String ids, HttpSession session) {
		try {
			for (String id : parseIds(ids)) {
				this.resumeBaseServiceImpl.deleteResumebaseEntity(id, getCurrentUser(session));
			}
			return ViewMode.getInstance().returnObjMode();
		} catch (Exception e) {
			log.error(e.getMessage());
			return ViewMode.getInstance().setError(e.getMessage()).returnObjMode();
		}
	}

	@RequestMapping({ "/list" })
	public ModelAndView index(HttpSession session) {
		return ViewMode.getInstance().returnModelAndView("resume/ResumebaseResult");
	}

	@RequestMapping({ "/form" })
	public ModelAndView view(RequestMode pageset, String ids) {
		try {
			switch (pageset.getOperateType()) {
			case 0:
				return

				ViewMode.getInstance().putAttr("pageset", pageset)
						.putAttr("entity", this.resumeBaseServiceImpl.getResumebaseEntity(ids))
						.returnModelAndView("resume/ResumebaseForm");

			case 1:
				return ViewMode.getInstance().putAttr("pageset", pageset).returnModelAndView("resume/ResumebaseForm");

			case 2:
				return

				ViewMode.getInstance().putAttr("pageset", pageset)
						.putAttr("entity", this.resumeBaseServiceImpl.getResumebaseEntity(ids))
						.returnModelAndView("resume/ResumebaseForm");
			}

			return ViewMode.getInstance().returnModelAndView("resume/ResumebaseForm");
		} catch (Exception e) {
			return ViewMode.getInstance().setError(e + e.getMessage()).returnModelAndView("resume/ResumebaseForm");
		}
	}
}
